﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalculatorFlexibe.Interfaces;

namespace CalculatorFlexibe.Implementations
{
    class CorrectValueInsert : IChanger
    {
        public string ChangeOnResult(int leftBoundary, int rightBoundary, string expr, double value)
        {
            if (leftBoundary != 0)
            {
                if (expr[leftBoundary - 1] == '-')
                    value *= -1;
                if (expr[leftBoundary - 1] == '-' || expr[leftBoundary - 1] == '+')
                    leftBoundary--;
            }
            string insert_part = "+";
            string value_str = string.Format("{0:0.000000}", value);
            if (value < 0) insert_part = value_str;
            else insert_part = insert_part + value_str;

            string right_str = "";
            string left_str = "";
            char[] array_right = new char[expr.Length - rightBoundary - 1];
            char[] array_left = new char[leftBoundary];
            if (rightBoundary != expr.Length - 1)
            {
                expr.CopyTo(rightBoundary + 1, array_right, 0, expr.Length - rightBoundary - 1);
                right_str = new string(array_right);
            }
            if (leftBoundary != 0)
            {
                expr.CopyTo(0, array_left, 0, leftBoundary);
                left_str = new string(array_left);
            }
            return left_str + insert_part + right_str;
        }
    }
}
